package net.helix.ui;


import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;


import net.helix.engine.Engine;
import net.helix.ui.panels.PropertiesPane;
import net.helix.ui.panels.ToolbarPane;
import net.helix.ui.panels.library.LibraryPane;
import net.helix.ui.panels.timeline.TimelinePane;
import net.helix.ui.panels.view.ViewPane;
import net.infonode.docking.DockingWindow;
import net.infonode.docking.FloatingWindow;
import net.infonode.docking.RootWindow;
import net.infonode.docking.SplitWindow;
import net.infonode.docking.TabWindow;
import net.infonode.docking.View;
import net.infonode.docking.properties.RootWindowProperties;
import net.infonode.docking.theme.DockingWindowsTheme;
import net.infonode.docking.theme.ShapedGradientDockingTheme;
import net.infonode.docking.util.DockingUtil;
import net.infonode.docking.util.PropertiesUtil;
import net.infonode.docking.util.ViewMap;
import net.infonode.util.Direction;

public class UIEngine {
	private static int untitledNumbering = 0;
	
	private RootWindowProperties rootProperties = new RootWindowProperties();
	private DockingWindowsTheme currentTheme = new ShapedGradientDockingTheme();
	
	private RootWindowProperties titleBarStyleProperties;
	
	private ArrayList<ViewPane> viewPanes;
	private ArrayList<View> viewViewPanes;

	private LibraryPane library;
	private View vLibrary;
	
	private ToolbarPane toolbar;
	private View vToolbar;
	
	private ArrayList<PropertiesPane> properties;
	private ArrayList<View> vProperties;
	
	private TimelinePane timeline;
	private View vTimeline;
	
	private ViewMap viewMap;
	
	private RootWindow rootWindow;
	
	private int windowCounter;
	private Engine engine;
	
	public UIEngine(Engine theEngine){
		this.engine = theEngine;
		
		engine.setUIEngine(this);
		windowCounter = 0;
		
		
		viewMap = new ViewMap();
		
		rootWindow = DockingUtil.createRootWindow(viewMap, true);
		
		viewPanes = new ArrayList<ViewPane>();
		viewViewPanes = new ArrayList<View>();
		
		//for(int i=0;i<3;i++){
			makeNewViewWindow();
		//}
		
		library = new LibraryPane(engine);
		vLibrary = new View("Library", null, library);
		viewMap.addView(windowCounter++, vLibrary);
		
		toolbar = new ToolbarPane(engine);
		vToolbar = new View("Toolbar", null, toolbar);
		viewMap.addView(windowCounter++, vToolbar);
		
		
		properties = new ArrayList<PropertiesPane>();
		vProperties = new ArrayList<View>();
		
		
		PropertiesPane properties0;
		View vProperties0;
		
		properties0 = new PropertiesPane(engine);
		vProperties0 = new View("Properties", null, properties0);
		viewMap.addView(windowCounter++, vProperties0);
		
		properties.add(properties0);
		vProperties.add(vProperties0);
		
		timeline = new TimelinePane(engine);
		vTimeline = new View("Timeline", null, timeline);
		viewMap.addView(windowCounter++, vTimeline);
		
		
		try{
			rootWindow.setWindow(getDefaultView());
			
			rootWindow.createFloatingWindow(new Point(0,0), new Dimension(400, 400), viewViewPanes.get(2));
			
		}catch(Exception e){
			
		}
				
		
		
		/*
		rootWindow.setWindow(new SplitWindow(true, .8f, 
				new SplitWindow(false, .2f, vTimeline, 
						new SplitWindow(false, .7f, new SplitWindow(true, .2f, vToolbar, 
								viewViewPanes[0]), 
								vProperties))
					, vLibrary));
		*/
		
		rootProperties.addSuperObject(currentTheme.getRootWindowProperties());
		rootWindow.getRootWindowProperties().addSuperObject(rootProperties);
		
		//rootProperties.getDockingWindowProperties().setCloseEnabled(false);
		//properties.getDockingWindowProperties().setMinimizeEnabled(false);
		//properties.getDockingWindowProperties().setMaximizeEnabled(false);
		//properties.getDockingWindowProperties().getTabProperties().getTitledTabProperties().setEnabled(false);
		
		
		titleBarStyleProperties = PropertiesUtil.createTitleBarStyleRootWindowProperties();
		rootProperties.addSuperObject(titleBarStyleProperties);
		
		//properties.getTabWindowProperties().getUndockButtonProperties().setVisible(false);
		
		rootProperties.getTabWindowProperties().getTabbedPanelProperties().setTabAreaOrientation(Direction.UP);
		
		
		rootWindow.getWindowBar(Direction.DOWN).setEnabled(true);
		rootWindow.getWindowBar(Direction.LEFT).setEnabled(true);
		rootWindow.getWindowBar(Direction.RIGHT).setEnabled(true);
		rootWindow.getWindowBar(Direction.UP).setEnabled(true);
		
		//rootWindow.getWindowBar(Direction.DOWN).addTab(vProperties);
		
		
		
		UIEngineTop.AddTabPane("Untitled "+(++untitledNumbering), rootWindow);
		
		
		// Frame showing layout for myMainRootWindow. 
		// Don't forget to remove this line when release compiling
		//DeveloperUtil.createWindowLayoutFrame("My Main RootWindow", rootWindow).setVisible(true);
		
		
		
		
		
		rootWindow.addComponentListener(new ComponentAdapter(){
			@Override
			public void componentHidden(ComponentEvent arg0) {
				engine.disableUpdateGraphics();
			}
			@Override
			public void componentShown(ComponentEvent arg0) {
				engine.enableUpdateGraphics();
				UIEngineTop.setMenu(getMenu());
				
			}
			
		});
		
		
		
		
		
		
	}
	public DockingWindow getDefaultView(){
		// vProperties
		
		TabWindow views = new TabWindow(new View[]{viewViewPanes.get(0)});
		views.setSelectedTab(0);
		
		
		
		return 
				new SplitWindow(false, .15f, vTimeline,
					new SplitWindow(true, 0.85f, 
							new SplitWindow(false, 0.8f, new SplitWindow(true, 0.1f, vToolbar, views), vProperties.get(0)), vLibrary)
							
				);
		
		
	}
	private int viewWindowCount = 0;
	public View makeNewViewWindow(){
		ViewPane viewPane = new ViewPane(engine);
		
		int count = (++viewWindowCount);
		
		viewPane = new ViewPane(engine);
		viewPane.setName("View "+count);
		
		View viewViewPane = new View("View "+count, null, viewPane);
		
		viewMap.addView(windowCounter++, viewViewPane);
		
		viewPanes.add(viewPane);
		viewViewPanes.add(viewViewPane);
		
		return viewViewPane;
		
	}
	public void popUpNewViewWindow(){
		FloatingWindow floatingWindow = rootWindow.createFloatingWindow(new Point(0,0), new Dimension(400, 400), makeNewViewWindow());
		
		floatingWindow.getTopLevelAncestor().setVisible(true);
	}
	public LibraryPane getCurrentLibraryPane(){
		return library;
	}
	public TimelinePane getCurrentTimelinePane(){
		return timeline;
	}
	
	
	
	public JMenuBar menu = null;
	public JMenuBar getMenu(){
		if(menu == null){
			menu = new JMenuBar();
			
			
			menu.add(UIEngineTop.getDefaultMenuFile());
			
			
			JMenu editMenu = new JMenu("Edit");
			editMenu.add("Undo");
			editMenu.add("Redo");
			menu.add(editMenu);
			
			JMenu windowsMenu = new JMenu("Windows");
			
			JMenuItem addNewView = new JMenuItem("Add new View");
			addNewView.addMouseListener(new MouseAdapter(){
				@Override
				public void mouseReleased(MouseEvent arg0) {
					popUpNewViewWindow();
					
				}
			});
			windowsMenu.add(addNewView);
			
			final JCheckBoxMenuItem visibleTimeline = new JCheckBoxMenuItem("Timeline");
			
			visibleTimeline.addMouseListener(new MouseAdapter(){
				@Override
				public void mouseReleased(MouseEvent arg0) {
					if(vTimeline.isShowing()){
						vTimeline.close();
					}else{
						vTimeline.restore();
					}
					
					
				}
			});
			windowsMenu.add(visibleTimeline);
			
			
			final JCheckBoxMenuItem visibleLibrary = new JCheckBoxMenuItem("Library");
			
			visibleLibrary.addMouseListener(new MouseAdapter(){
				@Override
				public void mouseReleased(MouseEvent arg0) {
					if(vLibrary.isShowing()){
						vLibrary.close();
					}else{
						vLibrary.restore();
					}
					
					
				}
			});
			windowsMenu.add(visibleLibrary);
			
			final JCheckBoxMenuItem visibleToolbar = new JCheckBoxMenuItem("Toolbar");
			
			visibleToolbar.addMouseListener(new MouseAdapter(){
				@Override
				public void mouseReleased(MouseEvent arg0) {
					if(vToolbar.isShowing()){
						vToolbar.close();
					}else{
						vToolbar.restore();
					}
					
				}
			});
			windowsMenu.add(visibleToolbar);
			
			windowsMenu.addMenuListener(new MenuListener(){

				@Override
				public void menuCanceled(MenuEvent arg0) {
				}

				@Override
				public void menuDeselected(MenuEvent arg0) {
				}

				@Override
				public void menuSelected(MenuEvent arg0) {
					visibleTimeline.setSelected(vTimeline.isShowing());
					visibleLibrary.setSelected(vLibrary.isShowing());
					visibleToolbar.setSelected(vToolbar.isShowing());
					
				}
				
			});
			
			
			menu.add(windowsMenu);
			
			menu.add(UIEngineTop.getDefaultMenuHelp());
		
		}
		
		return menu;
	}
}
